// b2c/hg/hg.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    https: "https://csb2c.fenxiangqu.cn/",
    ww: "100%",
    num:0,
    pood:[],
    astrict: 0,
    _id:null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: '换购商品',
    })
    const id = options.id,
    money = options.money;
    var a = wx.getStorageSync('cookie') || [];
    const self = this;
    this.setData({
      cookie: a,
      id:id,
      money: money
    })
    wx.request({
      url: "https://csb2c.fenxiangqu.cn/xcx_redemption_buy",
      data: {
        id: id
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Cookie': "LID=" + self.data.cookie
      },
      method: "POST",
      success: (e) => {
        if(e.data == "login"){
          wx.showModal({
            title: '提示',
            content: '您未登录，是否前往登录？',
            success: function (res) {
              if (res.confirm) {
                wx.redirectTo({
                  url: '../center/center',
                })
              } else if (res.cancel) {
                wx.redirectTo({
                  url: '../home/home',
                })
              }
            }
          })
        }else{
          wx.request({
            url: "https://csb2c.fenxiangqu.cn/xcx_amount",
            data: {
              id: id
            },
            header: {
              'content-type': 'application/x-www-form-urlencoded',
              'Cookie': "LID=" + self.data.cookie
            },
            method: "POST",
            success: (res) => {
              console.log(e.data)
              let json = e.data;
              let num = 0;
              for (let i = 0; i < json.addCommodity.length; i++){
                if (json.addCommodity[i].spkc){
                  json.addCommodity[i].exchangePrice = json.addCommodity[i].exchangePrice.toFixed(2);
                  if (json.addCommodity[i].chk == 1) {
                    num++;
                  }
                }
                
              }
              self.setData({
                pood: json.addCommodity,
                num:num,
                astrict: json.astrict,
                _id:json._id
              })
            },
            fail: (res) => {
              wx.showToast({
                title: '网络请求失败',
                icon: 'success',
                duration: 1000
              })
            }
          })
        }
      },
      fail: (res) => {
        wx.showToast({
          title: '网络请求失败',
          icon: 'success',
          duration: 1000
        })
      }
    })
  },
  chekcTap(e){
    const self = this;
    const ks = e.currentTarget.dataset;
    let chk = 0;
    let num = 1;
    let arr = [];
    let spid = ks.id;
    let hg_id = ks.d;
    let chkNum = ks.astrict;
    let chks = ks.chk;
    console.log('askdhaksjashdjkahsjkdhaksjdhsd')
    wx.request({
      url: "https://csb2c.fenxiangqu.cn/xcx_redemptionChk_init",
      data: {
        hg_id: hg_id
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Cookie': "LID=" + self.data.cookie
      },
      method: "POST",
      success: (res) => {
        if(res.data == "login"){
          wx.showModal({
            title: '提示',
            content: '您未登录，是否前往登录？',
            success: function (res) {
              if (res.confirm) {
                wx.redirectTo({
                  url: '../center/center',
                })
              } else if (res.cancel) {
                wx.redirectTo({
                  url: '../home/home',
                })
              }
            }
          })
        }else{
          for (let i = 0; i < res.data.addCommodity.length; i++ ){
            if (res.data.addCommodity[i].chk == 1){
              num++;
            }
          }
          if (chks == 1){
              num -= 1;
              chk = 0;
          }
          console.log(num)
          console.log(chkNum)
          if (num > chkNum){
          
            num -= 1;
            wx.showToast({
              title: `最多${chkNum}件`,
              icon: 'success',
              duration: 2000
            })
          }else{
            if (chks == 1){
              num -= 1;
              chk = 0;
            }else{
              num += 1;
              chk = 1;
            }
            wx.request({
              url: "https://csb2c.fenxiangqu.cn/xcx_redemptionChk",
              data: {
                id: spid,
                chk: chk,
                hg_id:hg_id
              },
              header: {
                'content-type': 'application/x-www-form-urlencoded',
                'Cookie': "LID=" + self.data.cookie
              },
              method: "POST",
              success: (e) => {
                if(e.data == "1"){
                  wx.request({
                    url: "https://csb2c.fenxiangqu.cn/xcx_redemption_buy",
                    data: {
                      id: self.data.id
                    },
                    header: {
                      'content-type': 'application/x-www-form-urlencoded',
                      'Cookie': "LID=" + self.data.cookie
                    },
                    method: "POST",
                    success: (es) => {
                      if (es.data == "login") {
                        wx.showModal({
                          title: '提示',
                          content: '您未登录，是否前往登录？',
                          success: function (res) {
                            if (res.confirm) {
                              wx.redirectTo({
                                url: '../center/center',
                              })
                            } else if (res.cancel) {
                              wx.redirectTo({
                                url: '../home/home',
                              })
                            }
                          }
                        })
                      } else {
                        wx.request({
                          url: "https://csb2c.fenxiangqu.cn/xcx_amount",
                          data: {
                            id: self.data.id
                          },
                          header: {
                            'content-type': 'application/x-www-form-urlencoded',
                            'Cookie': "LID=" + self.data.cookie
                          },
                          method: "POST",
                          success: (ress) => {
                            console.log(es.data)
                            let json = es.data;
                            let num = 0;
                            for (let i = 0; i < json.addCommodity.length; i++) {
                              if (json.addCommodity[i].spkc) {
                                json.addCommodity[i].exchangePrice = json.addCommodity[i].exchangePrice.toFixed(2);
                                if (json.addCommodity[i].chk == 1) {
                                  num++;
                                }
                              }

                            }
                            self.setData({
                              pood: json.addCommodity,
                              num: num,
                              astrict: json.astrict,
                              _id: json._id
                            })
                          },
                          fail: (res) => {
                            wx.showToast({
                              title: '网络请求失败',
                              icon: 'success',
                              duration: 1000
                            })
                          }
                        })
                      }
                    },
                    fail: (res) => {
                      wx.showToast({
                        title: '网络请求失败',
                        icon: 'success',
                        duration: 1000
                      })
                    }
                  })
                }else{
                  wx.showToast({
                    title: `抱歉，出现异常！`,
                    icon: 'success',
                    duration: 2000
                  })
                }
              },
              fail: (res) => {
                wx.showToast({
                  title: '网络请求失败',
                  icon: 'success',
                  duration: 1000
                })
              }
            })
          }
        }
      },
      fail: (res) => {
        wx.showToast({
          title: '网络请求失败',
          icon: 'success',
          duration: 1000
        })
      }
    })
  
  },
  btn(res){
    wx.showLoading({
      title: '加载中',
    })
    const self = this;
    wx.request({
      url: "https://csb2c.fenxiangqu.cn/xcx_redemption_addGwc",
      data: {
        xl:1,
        id: self.data.id
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Cookie': "LID=" + self.data.cookie
      },
      method: "POST",
      success: (res) => {
        if(res.data == "login"){
          wx.hideLoading()
          wx.showModal({
            title: '提示',
            content: '您未登录，是否前往登录？',
            success: function (res) {
              if (res.confirm) {
                wx.redirectTo({
                  url: '../center/center',
                })
              } else if (res.cancel) {
                wx.redirectTo({
                  url: '../home/home',
                })
              }
            }
          })
        }else{
          wx.hideLoading()
          if(res.data.error !=undefined){
            wx.showToast({
              title: '库存不足',
              icon: 'success',
              duration: 1500
            })
          }else{
            wx.navigateBack()
          }
        }
      },
      fail: (res) => {
        wx.hideLoading()
        wx.showToast({
          title: '网络请求失败',
          icon: 'success',
          duration: 1000
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})